package com.smh.myIoTest1;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test1 {
    public static void main(String[] args) throws IOException {
 /*
         制造假数据：
             获取姓氏：https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0
             获取男生名字：http://www.haoming8.cn/baobao/10881.html
             获取女生名字：http://www.haoming8.cn/baobao/7641.html
        */
        //1.定义变量
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";

        //2.获取数据
        String familyNameStr = webCrawler(familyNameNet);
        String boyNameStr = webCrawler(boyNameNet);
        String girlNameStr = webCrawler(girlNameNet);
//        System.out.println(familyNameStr);
//        System.out.println(boyNameStr);
//        System.out.println(girlNameStr);

        //通过正则表达式获取符合要求的数据
        ArrayList<String> familyNameTempList = getData(familyNameStr, "(([\\u4E00-\\u9FA5]){4})(，|。)", 1);
        //System.out.println(familyNameTempList);
        ArrayList<String> boyNameTempList = getData(boyNameStr, "([\\u4E00-\\u9FA5]{2})(、|。)", 1);
        //System.out.println(boyNameTempList);
        ArrayList<String> girlNameTempList = getData(girlNameStr, "(.. ){4}..", 0);//0是获取全部
        //System.out.println(girlNameTempList);

        //处理数据
        //姓氏
        StringBuilder sb = new StringBuilder();
        for (String s : familyNameTempList) {
            sb.append(s);
        }
        String[] familyName = sb.toString().split("");
        ArrayList<String> familyNameList = new ArrayList<>();
        for (String s : familyName) {
            familyNameList.add(s);
        }
        //System.out.println(familyNameList);
        //男名
        ArrayList<String> boyNameList = new ArrayList<>();
        for (String s : boyNameTempList) {
            if (!boyNameList.contains(s)) {
                boyNameList.add(s);
            }
        }
        //System.out.println(boyNameList);
        //女名
        ArrayList<String> girlNameList = new ArrayList<>();
        for (String s : girlNameTempList) {
            String[] split = s.split(" ");
            for (String s1 : split) {
                girlNameList.add(s1);
            }
        }
        //System.out.println(girlNameList);

//        生成数据
        ArrayList<String> infos = getInfos(familyNameList, boyNameList, girlNameList, 10, 10);
        System.out.println(infos);

        //写出数据
        /*FileWriter fw = new FileWriter("day28-code\\file\\names.txt");
        for (String info : infos) {
            fw.write(info+" ");
        }
        fw.close();*/
        BufferedWriter bw = new BufferedWriter(new FileWriter("day28-code\\file\\names.txt"));
        for (String info : infos) {
            bw.write(info);
            bw.newLine();
        }
        bw.close();


    }

    public static ArrayList<String> getInfos(ArrayList<String> familyNameList, ArrayList<String> boyNameList, ArrayList<String> girlNameList, int boyCount, int girlCount) {
        Random random = new Random();
        //男生名字()不重复
        HashSet<String> boyHs = new HashSet<>();
        while (true) {
            if (boyHs.size() == boyCount) {
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(boyNameList);
            boyHs.add(familyNameList.get(0) + boyNameList.get(0));
        }
        //女生名字(不重复)
        HashSet<String> girlHs = new HashSet<>();
        while (true) {
            if (girlHs.size() == girlCount) {
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(girlNameList);
            girlHs.add(familyNameList.get(0) + girlNameList.get(0));
        }
        //添加到数组中
        ArrayList<String> infos = new ArrayList<>();
        for (String boyH : boyHs) {
            infos.add(boyH + "-男-" + (random.nextInt(10) + 18));
        }
        for (String girlH : girlHs) {
            infos.add(girlH + "-女-" + (random.nextInt(8) + 18));
        }
        //ArrayList<String> infos =(ArrayList<String>)Stream.concat(boyHs.stream(), girlHs.stream()).collect(Collectors.toList());
        Collections.shuffle(infos);
        return infos;
    }
/*    public static ArrayList<String> getInfos(ArrayList<String> familyNameList,ArrayList<String> boyNameList,ArrayList<String> girlNameList){
        ArrayList<String> NameInfo = new ArrayList<>();
        Random random = new Random();
        for (String s : familyNameList) {
            for (String s1 : boyNameList) {
                NameInfo.add(s+s1+"-男-"+(random.nextInt(40)+20));
            }
            for (String s2 : girlNameList) {
                NameInfo.add(s+s2+"-女-"+(random.nextInt(40)+20));
            }
        }
        return NameInfo;
    }*/

    public static ArrayList<String> getData(String data, String regx, int index) {
        ArrayList<String> list = new ArrayList<>();
        //获取规则
        Pattern pattern = Pattern.compile(regx);
        //生成匹配器
        Matcher matcher = pattern.matcher(data);
        while (matcher.find()) {
            list.add(matcher.group(index));
        }
        return list;

    }

    public static String webCrawler(String net) throws IOException {
        StringBuilder sb = new StringBuilder();
        //创建网址对象
        URL url = new URL(net);
        //建立连接
        URLConnection conn = url.openConnection();

        //拿到输入流对象
        InputStream inputStream = conn.getInputStream();
        //转化流转成字符流,防止中文乱码.
        InputStreamReader isr = new InputStreamReader(inputStream);

        int ch;
        while ((ch = isr.read()) != -1) {
            sb.append((char) ch);
        }
        isr.close();
//        System.out.println(sb.toString());
        return sb.toString();
    }
}
